//
//  FindViewController.m
//  UGoods
//
//  Created by dllo on 16/1/15.
//  Copyright © 2016年 蓝鸥科技.李贺. All rights reserved.
//

#import "FindViewController.h"
#import "SelectionViewController.h"
#import "FreshViewController.h"
#import "BrandViewController.h"
#import "BaseSegmentedControl.h"
#import "WebDetailViewController.h"
#import "TopicsDetailViewController.h"
#import "FindModel.h"
#import "BuyDetailViewController.h"
#import "FeatureDetailViewController.h"
#import "BuyerDetailViewController.h"
#import "BrandDetailViewController.h"
#import "SearchViewController.h"
#import "ConstantsDefine.h"
@interface FindViewController ()

@property (nonatomic, retain) SelectionViewController *selection; // 精选页面
@property (nonatomic, retain) FreshViewController *fresh; // 鲜品
@property (nonatomic, retain) BrandViewController *brand; // 品牌

@end

@implementation FindViewController

- (void)dealloc
{
    [_selection release];
    [_fresh release];
    [_brand release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    // 分段控制器
    BaseSegmentedControl *segment = [[BaseSegmentedControl alloc] initWithItems:@[@"精选", @"鲜品", @"品牌"]];
    segment.frame = CGRectMake(0, 0, 200 * WIDTH, 30 * HEIGHT);
    segment.selectedSegmentIndex = 0;
//    segment.tintColor = [UIColor blackColor];
    self.navigationItem.titleView = segment;
    [segment release];
    
    [segment addTarget:self action:@selector(handleSegment:) forControlEvents:UIControlEventValueChanged];
    
    // 发现页
    // __block
    __block FindViewController *find = self;
    self.selection = [[SelectionViewController alloc] init];
    _selection.view.hidden = NO;
    [self.view addSubview:_selection.view];
    [_selection release];
    
    // 使用BLock传值 push跳转WebDetail页面
    
    self.selection.webDetail = ^(FindModel *model) {
        WebDetailViewController *web = [[WebDetailViewController alloc] init];
        web.model = model;
        [find.navigationController pushViewController:web animated:YES];
        [web release];
    };

    
    //
    // 使用BLock传值 push跳转TopDetail页面
    self.selection.topDetail = ^(FindModel *model) {
        TopicsDetailViewController *top = [[TopicsDetailViewController alloc] init];
        top.model = model;
        [find.navigationController pushViewController:top animated:YES];
        [top release];
    };
    
    // 使用BLock传值 push跳转BuyDetail页面
    self.selection.buyDetail = ^(FindModel *model) {
//        BuyDetailViewController *buy = [[BuyDetailViewController alloc] init];
//        buy.model = model;
//        [find.navigationController pushViewController:buy animated:YES];
        /********** 过年临时更换数据 **********/
        WebDetailViewController *web = [[WebDetailViewController alloc] init];
        web.model = model;
        [find.navigationController pushViewController:web animated:YES];
        [web release];
    };

    // feature详情页
    self.selection.featureDetail = ^(FindModel *model) {
        FeatureDetailViewController *feature = [[FeatureDetailViewController alloc] init];
        feature.model = model;
        [find.navigationController pushViewController:feature animated:YES];
        [feature release];
    };
    
    // buyer详情页
    self.selection.buyerDetail = ^(FindModel *model) {
        BuyerDetailViewController *buyer = [[BuyerDetailViewController alloc] init];
        buyer.model = model;
        [find.navigationController pushViewController:buyer animated:YES];
        [buyer release];
    };

    // 鲜品
    self.fresh = [[FreshViewController alloc] init];
    _fresh.view.hidden = YES;
    [self.view addSubview:_fresh.view];
    [_fresh release];
    
    // 使用BLock传值 push跳转BuyDetail页面
    self.fresh.buyDetailModelBlock = ^(FindModel *model) {
        BuyDetailViewController *buy = [[BuyDetailViewController alloc] init];
        buy.model = model;
        [find.navigationController pushViewController:buy animated:YES];
        [buy release];
    };

    // 品牌
    self.brand = [[BrandViewController alloc] init];
    _brand.view.hidden = YES;
    [self.view addSubview:_brand.view];
    [_brand release];
    
    // 使用BLock传值 push跳转BuyDetail页面
    self.brand.brandDetailModelBlock = ^(FindModel *model) {
        BrandDetailViewController *brand = [[BrandDetailViewController alloc] init];
        brand.model = model;
        [find.navigationController pushViewController:brand animated:YES];
        [brand release];
    };
    
    // 搜索按钮
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-search"] style:UIBarButtonItemStylePlain target:self action:@selector(handleSearch:)];
}

// 搜索按钮点击事件
- (void)handleSearch:(UIBarButtonItem *)rightBar {
    SearchViewController *search = [[SearchViewController alloc] init];
    [self.navigationController pushViewController:search animated:YES];
    [search release];
}

// 实现Segment页面切换效果
- (void)handleSegment:(UISegmentedControl *)segment {
    // 根据 selectedSegmentIndex 判断 根据view.hidden 实现页面切换
    if (segment.selectedSegmentIndex == 0) {
        _selection.view.hidden = NO;
        _fresh.view.hidden = YES;
        _brand.view.hidden = YES;
    } else if (segment.selectedSegmentIndex == 1) {
        _selection.view.hidden = YES;
        _fresh.view.hidden = NO;
        _brand.view.hidden = YES;
    } else if (segment.selectedSegmentIndex == 2) {
        _selection.view.hidden = YES;
        _fresh.view.hidden = YES;
        _brand.view.hidden = NO;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
